#include "LiquidDistributionChannelingDeviceController.h"
#include "EventHandler.h"
#include "Arduino.h"
LiquidDistributionChannelingDeviceController::LiquidDistributionChannelingDeviceController(IWaterHAL *HAL)
{
	this->HAL = HAL;

	isAtRegisteredWaterLevel = false;

//	onRequiredWaterLevelHandler->func = &LiquidDistributionChannelingDeviceController::onRequiredWaterLevel;
//	onRequiredWaterLevelHandler->inst = this;
//    HAL->onWaterLevelAtRegisteredLevel->addHandler(onRequiredWaterLevelHandler);
//
//    onPressureHighHandler->func = &LiquidDistributionChannelingDeviceController::onPressureHigh;
//    onPressureHighHandler->inst = this;
//    HAL->onWaterPressureHigh->addHandler(onPressureHighHandler);
//
//
//    onPressureLowHandler->func = &LiquidDistributionChannelingDeviceController::onPressureLow;
//    onPressureLowHandler->inst = this;
//    HAL->onWaterPressureLow->addHandler(onPressureLowHandler);
}

void LiquidDistributionChannelingDeviceController::setWaterLevel(int data)
{
	wantedWaterLevel = data;
	isAtRegisteredWaterLevel = false;
    HAL->registerWantedWaterLevel(data); //THIS IS ONLY DONE SO HAL WILL KNOW WHEN REQUIRED LEVEL HAS BEEN REACHED. THIS DOES NOT SET ANY HARDWARE THINGS.
    if(HAL->getWaterLevel() > data)
    {
        HAL->closeDrain();
        HAL->openSink();
    }
    else if(HAL->getWaterLevel() < data)
    {
        HAL->openDrain();
        HAL->closeSink();
    }
}

//ONLY TALK TO HAL IN TERMS OF:
/*
        void openSink();
        void closeSink();
        void openDrain();
        void closeDrain();

REGISTER EVENTS WITH THESE HAL EVENTPROPAGATORS:

        EventPropagator onInputOpen;
        EventPropagator onInputClose;
        EventPropagator onOutputOpen;
        EventPropagator onOutputClose;

*/


void LiquidDistributionChannelingDeviceController::onRequiredWaterLevel()
{
	Serial.println("CLOSING SINK AND DRAIN");
	isAtRegisteredWaterLevel = true;
    HAL->closeDrain();
    HAL->closeSink();
}

void LiquidDistributionChannelingDeviceController::onPressureLow()
{
    waterPressureHigh = false;
}

void LiquidDistributionChannelingDeviceController::onPressureHigh()
{
    waterPressureHigh = true;
}

bool LiquidDistributionChannelingDeviceController::getIsAtLevel()
{
	//return HAL->getIsWaterAtRegisteredLevel();
	return isAtRegisteredWaterLevel;
}